import axios from "axios";

import router from '../router/index'
import { createRouter, createWebHistory } from 'vue-router';
import { ElMessage } from 'element-plus'


// import { getCookie } from "./cookie";
// import Cookies from "js-cookie"
const service = axios.create({
    baseURL: "/api",
    timeout: 5000
})
service.interceptors.request.use(config => {
    // console.log(Cookies,"getcookie()");
    // const token = getCookie();
    // console.log(token);
    console.log(config, "config")
    let token = ""
    if (window.localStorage.getItem("userinfo")) {
        let obj = JSON.parse(window.localStorage.getItem("userinfo"));
        token = obj.token
    }
    if (token) {
        config.headers.token = token;

    }

    config.headers = {
        // 'Content-Type': config.type == 'json' || config.type != null ? 'application/json' : 'application/x-www-form-urlencoded;charset=UTF-8',
        'Content-Type': config.type == 'json' || config.type != null ? 'application/json' : 'application/x-www-form-urlencoded;charset=UTF-8',
        'X-Requested-With': 'XMLHttpRequest',
        "Accept": "application/json, text/plain, */*"
    }
    // if (config.type !== 'blob') {
    //     config.headers = {
    //         'Content-Type': config.type == 'json' || config.type != null ? 'application/json' : 'application/x-www-form-urlencoded;charset=UTF-8',
    //         'X-Requested-With': 'XMLHttpRequest',
    //     }
    // } else {
    //     config.headers = {
    //         'Content-Type': 'multipart/form-data',
    //         'X-Requested-With': 'XMLHttpRequest',
    //     }

    // }
    console.log(config.headers, "configheader")

    // let formData = new FormData();
    // console.log(config.data, "configdata")
    // for (let key in config.data) {
    //     console.log(key, "key")
    //     console.log(typeof (config.data[key]))
    //     if (typeof (config.data[key]) == "object") {
    //         config.data[key] = (JSON.stringify(config.data[key]))
    //     }
    //     formData.append(key, config.data[key] == undefined ? "" : config.data[key])
    //     console.log(formData, "formData")
    // }
    // console.log(formData, "formData1")
    // config.data = formData



    return config;
},
    error => {
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    response => {
        console.log(response, "response")
        if (response.data.msg == '登录超时，请重新登录') {
            router.push({
                path: "/"
            })
        }
        console.log(response.data)
        if (response.data.code == 500) {
            ElMessage({
                message: response.data.msg,
                type: 'warning',
                plain: true,
            })
        }
        return response.data
    },
    error => {
        console.log(router, "trouter")
        console.log(error, "error")
        // if (error.response.data.code == 400 || error.status == 400) {
        //     ElMessage({
        //         message: '参数错误',
        //         type: 'warning',
        //         plain: true,
        //     })
        // } else if (error.response.status == 401) {
        //     ElMessage({
        //         message: 'token已失效',
        //         type: 'warning',
        //         plain: true,
        //     })
        //     router.push({
        //         path: "/"
        //     })

        // }
        return Promise.reject(error)

    }
)
export default service